package com.aida.evil.eda.Login;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;

import com.aida.evil.aida.R;
import com.aida.evil.eda.Data.NetworkData;
import com.aida.evil.eda.Data.UserData;
import com.aida.evil.eda.Toast.CustomSuperActivityToast;
import com.aida.evil.eda.Volley.VolleyHandler;
import com.aida.evil.eda.Volley.VolleyHttpRequest;
import com.aida.evil.eda.Window.CustomDialog;
import com.aida.evil.eda.Window.PopupWindows;
import com.dd.CircularProgressButton;
import com.github.johnpersano.supertoasts.SuperActivityToast;
import com.github.johnpersano.supertoasts.SuperToast;
import com.github.johnpersano.supertoasts.util.OnClickWrapper;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;

/****************************************************
 * com.aida.evil.eda.Login.SetUser
 * 作者：白雨濃  evil
 * 时间：16-4-8 19:19
 * 邮箱：evilbai@foxmail.com
 * 页面用途：设置用户信息 包含(用户名,密码)
 ****************************************************/
public class SetUser extends AppCompatActivity
{
    private static final String SUBMITURL = "http://" + NetworkData.ServiceIP + "/UserUpdateData";

    private Toolbar toolbar;/*顶部栏*/

    private EditText username_EditText;/*用户名输入框*/
    private EditText oldpassword_EditText;/*密码输入框*/
    private EditText password_EditText;/*密码输入框*/
    private EditText repassword_EditText;/*再次输入密码*/

    private CircularProgressButton submit_button;/*提交按钮*/


    private String username, oldpassword, password, repassword;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setuser);

         /*动态加载背景图片，以免耗用ui线程时间*/
        Picasso.with(this).load(R.mipmap.login_background)
                .into((ImageView) findViewById(R.id.setuser_background));

        findviewById();/*查找控件*/
        {
            submit_button.setIndeterminateProgressMode(true);
        }
        setToolbar();/*设置顶部栏*/

        submitOnClick();/*设置提交按钮监听*/
    }

    /**
     * 查找控件
     */
    private void findviewById()
    {
        username_EditText = (EditText) findViewById(R.id.setuser_username);
        oldpassword_EditText = (EditText) findViewById(R.id.setuser_oldpassword);
        password_EditText = (EditText) findViewById(R.id.setuser_password);
        repassword_EditText = (EditText) findViewById(R.id.setuser_repassword);
        submit_button = (CircularProgressButton) findViewById(R.id.setuser_submit);
    }

    /**
     * 设置toolbar
     */
    private void setToolbar()
    {
        toolbar = (Toolbar) findViewById(R.id.setuser_toolbar);
        toolbar.setTitle("用户信息设置");
        setSupportActionBar(toolbar);//作用不明确
        getSupportActionBar().setHomeButtonEnabled(true);//作用不明确
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    /**
     * toolbar返回键监听
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if (item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 监听提交按钮
     */
    private void submitOnClick()
    {
        submit_button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (submit_button.getProgress() == -1)
                {
                    submit_button.setProgress(0);
                }
                else
                {
                    submit_button.setProgress(50);//让进度开始加载
                    getEditTextString();
                }
            }
        });
    }

    /**
     * 网络加载
     */
    private void toNetWork()
    {
        VolleyHandler<String> stringVolleyHandler = new VolleyHandler<String>()
        {
            @Override
            public void reqSuccess(String response)
            {
                if (response.endsWith("true"))
                {
                    submit_button.setProgress(100);
                    UserData.DATA_PASSWORD = password;/*更新本地密码*/
                    final CustomDialog.Builder builder = new CustomDialog.Builder(SetUser.this);
                    builder.setTitle("提示");
                    builder.setMessage("注册成功!");
                    builder.setCanceledOnTouchOutside(false);
                    builder.setBackPressed(false);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            builder.dismiss();
                        }
                    });
                    builder.show();
                }
                else
                {
                    submit_button.setProgress(-1);
                    final CustomDialog.Builder builder = new CustomDialog.Builder(SetUser.this);
                    builder.setTitle("提示");
                    builder.setMessage("注册失败!\n" + response);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            builder.dismiss();
                        }
                    });
                    builder.show();
                }
            }

            @Override
            public void reqError(String error)
            {
                submit_button.setProgress(-1);
            }
        };
        Map map = new HashMap();
        map.put("phonenum", UserData.DATA_PHONENUM);
        map.put("username", username);
        map.put("password", password);/*通过md5加密*/
        VolleyHttpRequest.String_request(SUBMITURL, map, stringVolleyHandler);
    }

    /**
     * 得到输入框内的内容
     * if 1:判断是否为空
     * if 2:判断密码长度
     * if 3:判断重复密码是否与原密码相同
     */
    private void getEditTextString()
    {
        username = username_EditText.getText().toString();
        oldpassword = Encrypt_MD5.GetMD5Code(oldpassword_EditText.getText().toString());
        password = Encrypt_MD5.GetMD5Code(password_EditText.getText().toString());
        repassword = Encrypt_MD5.GetMD5Code(repassword_EditText.getText().toString());

        if (!username_EditText.getText().toString().isEmpty()
                && !oldpassword_EditText.getText().toString().isEmpty()
                && !password_EditText.getText().toString().isEmpty()
                && !repassword_EditText.getText().toString().isEmpty())
        {
            /*判断密码长度*/
            if (password_EditText.getText().toString().length() > 5)
            {
                /*判断两次输入密码是否一致*/
                if (!password.endsWith(repassword))
                {
                    CustomSuperActivityToast.makeText(this, "两次密码输入不一致!", "确认", new SuperToast.OnClickListener()
                    {
                        @Override
                        public void onClick(View view, Parcelable token)
                        {
                            submit_button.setProgress(0);
                        }
                    }).show();
                }
                else
                {
                    /*判断是否与原密码相同*/
                    if (oldpassword.equals(UserData.DATA_PASSWORD))
                    {
                        toNetWork();
                    }
                    else
                    {
                        submit_button.setProgress(0);
                        CustomSuperActivityToast.makeText(this, "与原密码不一致", "确认").show();
                    }
                }
            }
            else
            {
                PopupWindows popupWindows = new PopupWindows(SetUser.this
                        , findViewById(R.id.setuser)
                        , new String[]{"密码长度过短\n请重新输入!"}, new String[]{"确定"})
                {//显示底部框
                    @Override
                    public void listView_1_OnClick(AdapterView<?> parent, View view, int position, long id)
                    {

                    }

                    @Override
                    public void listView_1_LongOnClick(AdapterView<?> parent, View view, int position, long id)
                    {

                    }

                    @Override
                    public void listView_2_OnClick(AdapterView<?> parent, View view, int position, long id)
                    {
                        submit_button.setProgress(0);
                        dismiss();
                    }

                    @Override
                    public void listView_2_LongOnClick(AdapterView<?> parent, View view, int position, long id)
                    {

                    }
                };
                popupWindows.setTouchDismiss(true);
            }
        }
        else
        {
            PopupWindows popupWindows = new PopupWindows(SetUser.this
                    , findViewById(R.id.setuser)
                    , new String[]{"部分输入为空\n请重新输入!"}, new String[]{"确定"})
            {//显示底部框
                @Override
                public void listView_1_OnClick(AdapterView<?> parent, View view, int position, long id)
                {

                }

                @Override
                public void listView_1_LongOnClick(AdapterView<?> parent, View view, int position, long id)
                {

                }

                @Override
                public void listView_2_OnClick(AdapterView<?> parent, View view, int position, long id)
                {
                    submit_button.setProgress(0);
                    dismiss();
                }

                @Override
                public void listView_2_LongOnClick(AdapterView<?> parent, View view, int position, long id)
                {

                }
            };
            popupWindows.setTouchDismiss(true);
        }
    }
}
